package com.milk.web.controller.admin;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.dubbo.annotation.AutowiredDubbo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.milk.model.entity.admin.Admin;
import com.milk.service.admin.AdminService;
import com.milk.utils.CookieUtils;
import com.milk.web.config.SystemConfig;
import com.xiaoleilu.hutool.crypto.SecureUtil;
import com.xiaoleilu.hutool.util.ObjectUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


@RestController
@RequestMapping("/manager/admin")
public class AdminController {
	
	private Logger log = LoggerFactory.getLogger(getClass());

	@AutowiredDubbo(check = false)
	private AdminService adminService;
	
	@ApiOperation(value="获取管理员列表",notes="传入参数{params}，返回jsonarray")
	@ApiImplicitParam(dataType="java.util.Map",name="params",value="请求参数，如姓名，用户名等",paramType="query")
	@RequestMapping("/list.do")
	public String list(Map<String,Object> params){
		
		if(ObjectUtil.isNull(params.get("startPageRowCount"))){
			params.put("startPageRowCount", 0);
		}
		if(ObjectUtil.isNull(params.get("maxPageRowCount"))){
			params.put("maxPageRowCount", 10);
		}
		List<Admin> admins = adminService.query(params);
		String result = "";
		if(ObjectUtil.length(admins)>0){
			result = JSONArray.toJSONString(admins);
		}
		return result;
	}
	
	@ApiOperation(value="新增管理员",notes="传入参数Admin，返回0或1")
	@ApiImplicitParam(dataType="Admin",name="admin",value="传入管理员信息",paramType="query")
	@RequestMapping("/save.do")
	public String save(Admin admin){
		String result = "0";
		try{
			adminService.save(admin);
			result = "1";
		}catch(Exception e){
			log.error(e.getMessage());
		}
		return result;
	}
	
	@ApiOperation(value="编辑管理员",notes="传入参数Admin，返回0或1")
	@ApiImplicitParam(dataType="Admin",name="admin",value="传入管理员信息",paramType="query")
	@RequestMapping("/update.do")
	public String update(Admin admin){
		String result = "0";
		try{
			adminService.update(admin);
			result = "1";
		}catch(Exception e){
			log.error(e.getMessage());
		}
		return result;
	}
	
	@ApiOperation(value="删除管理员",notes="传入参数Admin，返回0或1")
	@ApiImplicitParam(dataType="long",name="adminId",value="传入管理员ID",paramType="query")
	@RequestMapping("/delete.do")
	public String delete(long adminId){
		String result = "0";
		try{
			adminService.delete(adminId);
			result = "1";
		}catch(Exception e){
			log.error(e.getMessage());
		}
		return result;
	}
	
	@ApiOperation(value="管理员登陆",notes="传入参数username，password，异常返回0，失败返回原因，正常返回1")
	@ApiImplicitParams({
		@ApiImplicitParam(dataType="java.lang.String",name="username",value="用户名",paramType="query"),
		@ApiImplicitParam(dataType="java.lang.String",name="password",value="密码",paramType="query")
	})
	@RequestMapping("/login.do")
	public String login(HttpServletResponse response,String username,String password){
		String result = "0";
		try{
			Admin admin = adminService.findByUserName(username);
			if(ObjectUtil.isNotNull(admin)){
				if(admin.getPassword().equals(SecureUtil.md5(password))){
					result = "1";
					CookieUtils.setEncodeCookieValue(response, SystemConfig.ADMIN_COOKIE_ID, String.valueOf(admin.getId()), SystemConfig.HOST, 12*3600);
				}else{
					result = "密码不正确";
				}
			}else{
				result = "用户名不存在";
			}
			
		}catch(Exception e){
			log.error(e.getMessage());
		}
		return result;
	}
	
	@RequestMapping("/logout.do")
	public void logout(HttpServletRequest request,HttpServletResponse response){
		
		try{
			CookieUtils.delCookie(response,SystemConfig.ADMIN_COOKIE_ID,SystemConfig.HOST);
		}catch(Exception e){
			log.error(e.getMessage());
		}
	}
	
	
}
